package com.byl.modules.sys.controller;

import com.byl.common.modules.controller.BaseController;
import com.byl.modules.sys.service.RoleService;
import com.byl.sdk.bean.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/12/5.
 */
@RestController
@RequestMapping("/api/sys/role")
public class RoleController extends BaseController {

    @Autowired
    RoleService roleService;

    @PostMapping("/list")
    public String queryRole(@RequestBody Map map) {
        ResultBean bean = roleService.query(map);
        return success("角色列表", bean);
    }

    @PostMapping("/save")
    public String save(@RequestBody Map map) {
        if (!map.containsKey("menus") || "".equals(map.get("menus")))
            return fail("角色权限不能为空！");
        int rs = roleService.save(map);
        if (rs > 0)
            return success("保存成功", "");
        return fail("保存失败");
    }

    @PostMapping("/update")
    public String update(@RequestBody Map map) {
        int rs = roleService.update(map);
        if (rs > 0)
            return success("更新成功", "");
        return fail("更新失败");
    }

    @GetMapping("/delete")
    public String delete(String id) {
        String rs = roleService.deleteRole(id);
        if ("success".equals(rs)) {
            return success("删除成功", "");
        } else if ("fail".equals(rs)) {
            return fail("删除失败");
        } else {
            return fail("用户"+rs+"正在使用该角色！");
        }
    }

    @GetMapping("/name/check")
    public String checkRole(String role_name) {
        int rs = roleService.checkRole(role_name);
        return success("检查角色是否存在", rs);
    }

    @GetMapping("/get")
    public String getRole(String id){
        Map map=roleService.getMapById(id);
        return success("角色详情",map);
    }

    @GetMapping("/queryall")
    public String queryAllRole(){
        List list=roleService.queryAllRole();
        return success("所有角色",list);
    }
}
